﻿// 多球反弹.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include <graphics.h>
#include <conio.h>


int main()
{
	initgraph(640, 480);
	int ball_x[15];
	int ball_y[15];
	int ball_vx[15], ball_vy[15];
	int radius = 20;
	int i;
	int j;
	BeginBatchDraw();
	for (i = 0;i < 15;i++)
	{
		ball_x[i] = rand() % 600 + 20;
		ball_y[i] = rand() % 440 + 20;
		ball_vx[i] =1;
		ball_vy[i] =1;
	}
while (1)
{
	setcolor(YELLOW);
	setfillcolor(GREEN);
	for (i = 0; i < 15; i++)
	{
		fillcircle(ball_x[i], ball_y[i], radius);
	}
	FlushBatchDraw();
	Sleep(3);
	setcolor(BLACK);
	setfillcolor(BLACK);
	for (i = 0; i < 15; i++)
	{
		fillcircle(ball_x[i], ball_y[i], radius);
	}
	for (i = 0; i < 15; i++)
	{
		ball_x[i] = ball_x[i] + ball_vx[i];
		ball_y[i] = ball_y[i] + ball_vy[i];
		if (ball_x[i] == 620 || ball_x[i] == 20)
			ball_vx[i] = -ball_vx[i];
		if (ball_y[i] == 460 || ball_y[i] == 20)
			ball_vy[i] = -ball_vy[i];
		for (j = 0; j < 15; j++)
		{
			if (j != i)
			{
				if (((ball_x[i] - ball_x[j])*(ball_x[i] - ball_x[j]) + (ball_y[i] - ball_y[j])*(ball_y[i] - ball_y[j])) < 1600)
				{
					ball_vx[i] = -ball_vx[i];
					ball_vy[i] = -ball_vy[i];
					ball_vx[j] = -ball_vx[j];
					ball_vy[j] = -ball_vy[j];
				}
			}
		}
	}
}
	EndBatchDraw();
	closegraph();
}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门提示: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件，或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来，若要再次打开此项目，请转到“文件”>“打开”>“项目”并选择 .sln 文件
